package com.example.administrator.u17.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.modle.SearchBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Smile on 2016/11/15.
 */

public class SearchAdapter extends BaseAdapter {
    private ArrayList<SearchBean.RankingList> mData;
    private Context mContext;

    public SearchAdapter(Context mContext, ArrayList<SearchBean.RankingList> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData==null?0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.search_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        Picasso.with(mContext).load(mData.get(position).cover).into(holder.imageView);
        holder.textView.setText(mData.get(position).sortName);
        return convertView;
    }
    class ViewHolder{
        ImageView imageView;
        TextView textView;
        public ViewHolder(View view){
            imageView= (ImageView) view.findViewById(R.id.iv_search_item);
            textView= (TextView) view.findViewById(R.id.tv_search_item);
        }
    }
}
